# -*- coding: utf-8 -*-
from cv2 import cv2
import numpy as np

A = cv2.imread('apple.png')
# 注意：为了使后面可以逐渐减半，这里的尺寸必须为2的次幂
A = cv2.resize(A, (256, 256), interpolation=cv2.INTER_CUBIC)
B = cv2.imread('orange.png')
B = cv2.resize(B, (256, 256), interpolation=cv2.INTER_CUBIC)
# 生成高斯金字塔
G = A.copy()
gpA = [G]
for i in range(5):
    G = cv2.pyrDown(G)
    gpA.append(G)

G = B.copy()
gpB = [G]
for i in range(5):
    G = cv2.pyrDown(G)
    gpB.append(G)

# 产生Laplacian金字塔
lpA = [gpA[5]]
for i in range(5, 0, -1):
    GE = cv2.pyrUp(gpA[i])
    L = cv2.subtract(gpA[i - 1], GE)
    lpA.append(L)

lpB = [gpB[5]]
for i in range(5, 0, -1):
    GE = cv2.pyrUp(gpB[i])
    L = cv2.subtract(gpB[i - 1], GE)
    lpB.append(L)

# 合并
LS = []
for la, lb in zip(lpA, lpB):
    rows, cols, dpt = la.shape
    ls = np.hstack((la[:, 0:cols // 2], lb[:, cols // 2:]))
    LS.append(ls)

# 重新构建图像
ls_ = LS[0]
for i in range(1, 6):
    ls_ = cv2.pyrUp(ls_)
    ls_ = cv2.add(ls_, LS[i])

# 连接
real = np.hstack((A[:, :cols // 2], B[:, cols // 2:]))

cv2.imshow("LS", ls_)
cv2.imshow("Real", real)

cv2.waitKey()
cv2.destroyAllWindows()
